package grpc

import (
	pb "bilibi.com/comment/api"
	"bilibi.com/comment/conf"
	"bilibi.com/comment/service"
	log "github.com/sirupsen/logrus"
	"google.golang.org/grpc"
	"google.golang.org/grpc/keepalive"
	"net"
	"time"
)

func Init(c *conf.Config, svr *service.Service) {
	log.Info("grpc: start listen addr:" + c.CommentRPCAddr)

	lis, err := net.Listen("tcp", c.CommentRPCAddr)
	if err != nil {
		panic(err)
	}

	grpcServer := grpc.NewServer(grpc.KeepaliveParams(keepalive.ServerParameters{Time: time.Second * 10}))
	pb.RegisterCommentServer(grpcServer, &server{svr})

	// Run the server
	if err := grpcServer.Serve(lis); err != nil {
		panic(err)
	}

}

type server struct {
	svr *service.Service
}
